package com.yhq.ojbackendjudgeservice.judge.stratege;

import cn.hutool.json.JSONUtil;
import com.yhq.ojbackendmodel.model.codesandbox.JudgeInfo;
import com.yhq.ojbackendmodel.model.dto.question.JudgeCase;
import com.yhq.ojbackendmodel.model.dto.question.JudgeConfig;
import com.yhq.ojbackendmodel.model.entity.Question;
import com.yhq.ojbackendmodel.model.enums.JudgeInfoEnum;

import java.util.List;
import java.util.Optional;

/**
 * Java 程序的判题策略
 */
public class JavaLanguageJudgeStrategy implements JudgeStrategy {

    /**
     * 执行判题
     *
     * @param judgeContext
     * @return
     */
//    private static final int defaultMemory = 100*1000*1000;
//    private static final int defaultTime = 1*1000*1000;

    @Override
    public JudgeInfo doJudge(JudgeContext judgeContext) {
        JudgeInfo judgeInfo = judgeContext.getJudgeInfo();
        if(judgeInfo == null){
            throw new RuntimeException("判题信息错误");
        }
        Integer memory = Optional.ofNullable(judgeInfo.getMemory()).orElse(0) ;
        Integer time = Optional.ofNullable(judgeInfo.getTime()).orElse(0);

        List<String> inputList = judgeContext.getInputList();
        List<String> outputList = judgeContext.getOutputList();
        Question question = judgeContext.getQuestion();
        List<JudgeCase> judgeCaseList = judgeContext.getJudgeCaseList();
        JudgeInfoEnum judgeInfoEnum = JudgeInfoEnum.ACCEPTED;
        JudgeInfo judgeInfoResponse = new JudgeInfo();
        judgeInfoResponse.setMemory(memory);
        judgeInfoResponse.setTime(time);
        // 先判断沙箱执行的结果输出数量是否和预期输出数量相等
        if (outputList.size() != inputList.size()) {
            judgeInfoEnum = JudgeInfoEnum.WRONG_ANSWER;
            judgeInfoResponse.setMessage(judgeInfoEnum.getStatus());
            return judgeInfoResponse;
        }
        // 依次判断每一项输出和预期输出是否相等
        for (int i = 0; i < judgeCaseList.size(); i++) {
            JudgeCase judgeCase = judgeCaseList.get(i);
            if (!judgeCase.getOutput().equals(outputList.get(i))) {
                judgeInfoEnum = JudgeInfoEnum.WRONG_ANSWER;
                judgeInfoResponse.setMessage(judgeInfoEnum.getStatus());
                return judgeInfoResponse;
            }
        }
        // 判断题目限制
        String judgeConfigStr = question.getJudgeConfig();
        JudgeConfig judgeConfig = JSONUtil.toBean(judgeConfigStr, JudgeConfig.class);
        Integer needMemoryLimit = judgeConfig.getMemoryLimit();
        Integer needTimeLimit = judgeConfig.getTimeLimit();
        if (memory > needMemoryLimit) {
            judgeInfoEnum = JudgeInfoEnum.OUT_OF_MEMORY;
            judgeInfoResponse.setMessage(judgeInfoEnum.getStatus());
            return judgeInfoResponse;
        }
        // Java 程序本身需要额外执行 10 秒钟
        long JAVA_PROGRAM_TIME_COST = 10000L;
        if ((time - JAVA_PROGRAM_TIME_COST) > needTimeLimit) {
            judgeInfoEnum = JudgeInfoEnum.TIME_LIMITED_EXCEPTION;
            judgeInfoResponse.setMessage(judgeInfoEnum.getStatus());
            return judgeInfoResponse;
        }
        judgeInfoResponse.setMessage(judgeInfoEnum.getStatus());
        return judgeInfoResponse;
    }
}
